#include <QByteArray>
#include "custom_message_box.h"
#include "config_ip_address_dialog.h"
#include "ui_config_ip_address_dialog.h"
#include "size_para.h"
#include "global_variable.h"
#include "gsettings_monitor.h"
#include "cups_debug_logging_checkbox.h"

#include <QObject>

ConfigIPAddressDialog::ConfigIPAddressDialog(QWidget *parent) :
    CustomDialog(parent),
    m_ui(new Ui::ConfigIPAddressDialog),
    m_isConfigStatusOK(false)
{
    // setWindowModality(Qt::ApplicationModal);
    m_ui->setupUi((QDialog *)this->mainWidget());
    m_ui->ipAddress->setAcceptDrops(false);
    setWindowTitle(tr("Configure"));
    setFixedSize(420,252);
    m_ui->serviceAddressLabel->setText(tr("Service address:"));
    m_ui->cancelBtn->setText(tr("Cancel"));
    m_ui->applyBtn->setText(tr("Ok"));
    m_cupsDebug = m_ui->cupsCheckBox->isChecked();
    this->show();
    if(GsettingsMonitor::getInstance()->gsettingsCheck())
    {
        qDebug() << "success";
        m_isConfigStatusOK = true;
    }
    else
    {
        qDebug() << "fail";
        //弹窗
        CustomMessageBox Msg(CustomMessageBox::Warning, tr("Warning"), tr("Config init failed"),
                        CustomMessageBox::NoButton);
        Msg.setParent(this);
        QPushButton *btnYes = Msg.addButton(tr("Confirm"), CustomMessageBox::YesRole);
        Msg.exec();
        if (Msg.clickedButton() == btnYes)
        {
            m_isConfigStatusOK = false;
            this->close();
            return;
        }
    }

    loadSettings();

    connect(m_ui->ipAddress, &QLineEdit::textChanged, this, &ConfigIPAddressDialog::onSettingsChanged);
    connect(m_ui->cupsCheckBox, &CupsDebugLoggingCheckbox::stateChanged, this, &ConfigIPAddressDialog::onSettingsChanged);
    connect(m_ui->applyBtn, SIGNAL(released()), this, SLOT(onApplyBtnClicked()));
    connect(m_ui->cancelBtn,SIGNAL(released()),this,SLOT(onCancelBtnClicked()));
}

ConfigIPAddressDialog::~ConfigIPAddressDialog()
{
    delete m_ui;
}

void ConfigIPAddressDialog::loadSettings()
{
    QString str = GsettingsMonitor::getInstance()->apiServerAddressGet();
    m_oldIpAddress = str;
    qDebug() << "now api web is :" << str;
    if (m_ui->ipAddress)
    {
        m_ui->ipAddress->setText(str);
    }
    m_ui->applyBtn->setEnabled(false);
}

void ConfigIPAddressDialog::saveSettings()
{
    QString str = (m_ui->ipAddress) ? m_ui->ipAddress->text() : "";
    GsettingsMonitor::getInstance()->apiServerAddressSet(str);
    qDebug() << "更换地址为 :" << str;
}

void ConfigIPAddressDialog::onApplyBtnClicked()
{
    qDebug() << "按下了应用键，修改服务器地址！";
    saveSettings();
    m_ui->cupsCheckBox->saveDebugLogging();
    this->close();
}

void ConfigIPAddressDialog::onCancelBtnClicked()
{
    qDebug() << "按下了取消键，直接退出！";
    this->close();
}

bool ConfigIPAddressDialog::getConfigStatus()
{
    return m_isConfigStatusOK;
}

void ConfigIPAddressDialog::onSettingsChanged()
{
    if (m_cupsDebug == m_ui->cupsCheckBox->isChecked() && m_oldIpAddress == m_ui->ipAddress->text()) {
        m_ui->applyBtn->setEnabled(false);
    } else {
        m_ui->applyBtn->setEnabled(true);
    }
}
